package com.xiaofine.springbootmongodb;

import com.xiaofine.springbootmongodb.dao.CustomerRepository;
import com.xiaofine.springbootmongodb.entity.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootMongodbApplication implements CommandLineRunner {

    @Autowired
    private CustomerRepository customerRepository;

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMongodbApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        customerRepository.deleteAll();

        customerRepository.save(new Customer("Alice","Smith"));
        customerRepository.save(new Customer("Bob","Smith"));
        System.out.println("Customers found with findAll():");
        System.out.println("-------------------------------");
        for (Customer customer : customerRepository.findAll()) {
            System.out.println(customer);
        }

        System.out.println();


        System.out.println("Customers found with findByFirstName():");
        System.out.println("-------------------------------");
        System.out.println(customerRepository.findByFirstName("Alice"));

        System.out.println("Customers found with findByLastName():");
        System.out.println("-------------------------------");
        for(Customer customer : customerRepository.findByLastName("Smith")){
            System.out.println(customer);
        }
    }
}
